// Test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc\sax\HandlerBase.hpp>
#include <iostream>
#include <xercesc\framework\StdOutFormatTarget.hpp>
#include <xercesc\framework\LocalFileFormatTarget.hpp>
#include "xercesHelper.h"

using namespace std;
using namespace xercesc;
using namespace xercescHelpingLibrary;

//void getMaterial( DOMElement* element ) 
//{
//	cout<<"Material:   ";
//	cout<<xhlGetAttributeValue(element, "MaterialName")<<endl;
//}
//
//void LoadMaterialPairs(DOMElement* element) 
//{
//	cout<<"MaterialPairs:   ";
//	cout<<xhlGetAttributeValue(element, "Material1")<<", ";
//	cout<<xhlGetAttributeValue(element, "Material2")<<endl;
//}
//
//void getElement( DOMElement* element )
//{
//	if (element==NULL) return;
//
//	string nodeName = xhlXMLChToString(element->getNodeName());
//	if (nodeName=="Material")
//	{
//		getMaterial(element);
//	}
//	else if (nodeName=="MaterialPairs")
//	{
//		LoadMaterialPairs(element);
//	}
//}
//
//int serializeDOM(DOMNode* node) {
//
//	XMLCh tempStr[100];
//	XMLString::transcode("LS", tempStr, 99);
//	DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(tempStr);
//	DOMLSSerializer* theSerializer = ((DOMImplementationLS*)impl)->createLSSerializer();
//
//	// optionally you can set some features on this serializer
//	if (theSerializer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTDiscardDefaultContent, true))
//		theSerializer->getDomConfig()->setParameter(XMLUni::fgDOMWRTDiscardDefaultContent, true);
//
//	if (theSerializer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
//		theSerializer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
//
//	//// optionally you can implement your DOMLSSerializerFilter (e.g. MyDOMLSSerializerFilter)
//	//// and set it to the serializer
//	//DOMLSSerializer* myFilter = new myDOMLSSerializerFilter();
//	//theSerializer->setFilter(myFilter);
//
//	//// optionally you can implement your DOMErrorHandler (e.g. MyDOMErrorHandler)
//	//// and set it to the serializer
//	//DOMErrorHandler* errHandler = new myDOMErrorHandler();
//	//theSerializer->getDomConfig()->setParameter(XMLUni::fgDOMErrorHandler, myErrorHandler);
//
//	// StdOutFormatTarget prints the resultant XML stream
//	// to stdout once it receives any thing from the serializer.
//	//XMLFormatTarget *myFormTarget = new StdOutFormatTarget();
//	XMLFormatTarget *myFormTarget = new LocalFileFormatTarget("aa.xml");
//
//
//	DOMLSOutput* theOutput = ((DOMImplementationLS*)impl)->createLSOutput();
//	theOutput->setByteStream(myFormTarget);
//
//	try {
//		// do the serialization through DOMLSSerializer::write();
//		theSerializer->write(node, theOutput);
//	}
//	catch (const XMLException& toCatch) {
//		char* message = XMLString::transcode(toCatch.getMessage());
//		cout << "Exception message is: \n"
//			<< message << "\n";
//		XMLString::release(&message);
//		return -1;
//	}
//	catch (const DOMException& toCatch) {
//		char* message = XMLString::transcode(toCatch.msg);
//		cout << "Exception message is: \n"
//			<< message << "\n";
//		XMLString::release(&message);
//		return -1;
//	}
//	catch (...) {
//		cout << "Unexpected Exception \n" ;
//		return -1;
//	}
//
//	theOutput->release();
//	theSerializer->release();
//	//delete myErrorHandler;
//	//delete myFilter;
//	delete myFormTarget;
//	return 0;
//}
//
//
//int _tmain2(int argc, _TCHAR* argv[])
//{
//	try {
//		XMLPlatformUtils::Initialize();
//	}
//	catch (const XMLException&) {
//		return -1;
//	}
//
//	XercesDOMParser* parser = new XercesDOMParser();
//	ErrorHandler* errHandler = (ErrorHandler*) new HandlerBase();
//	parser->setErrorHandler(errHandler);
//
//	try {
//		DOMImplementation* implementation = DOMImplementation::getImplementation();
//		DOMDocument* doc = implementation->createDocument();
//
//		DOMElement* element1 = xhlCreateElement(doc, "testElement1");
//		DOMElement* element2 = xhlCreateElement(doc, "testElement2");
//		DOMElement* element3 = xhlCreateElement(doc, "testElement3");
//		DOMElement* element  = xhlCreateElement(doc, "Foooo");
//		
//		element->appendChild(element1);
//		element->appendChild(element2);
//		element->appendChild(element3);
//
//		serializeDOM(element);
//	}
//	catch (const XMLException& toCatch) {
//		char* message = XMLString::transcode(toCatch.getMessage());
//		cout<< "Exception message is: \n"
//			<< message << "\n";
//		XMLString::release(&message);
//		return NULL;
//	}
//	catch (const DOMException& toCatch) {
//		char* message = XMLString::transcode(toCatch.msg);
//		cout<< "Exception message is: \n"
//			<< message << "\n";
//		XMLString::release(&message);
//		return NULL;
//	}
//	catch (...) {
//		cout<< "Unexpected Exception \n" ;
//		return NULL;
//	}
//
//	delete parser;
//	delete errHandler;
//	return NULL;
//
//	return 0;
//}


int _tmain1(int argc, _TCHAR* argv[])
{
	

	//////XercesDOMParser* parser = new XercesDOMParser();
	//////ErrorHandler* errHandler = (ErrorHandler*) new HandlerBase();
	//////parser->setErrorHandler(errHandler);

	//////try {
	//////	parser->setValidationScheme(XercesDOMParser::Val_Always);
	//////	parser->setDoNamespaces(true);
	//////	parser->setDoSchema(true);
	//////	parser->setExternalNoNamespaceSchemaLocation("sample.xsd");
	//////	parser->parse("sample.xml");
	//////	DOMDocument* xmlDoc = parser->getDocument();
	//////	DOMNode* root = xmlDoc->getDocumentElement(); 

	//////	
	//////	cout<<"Root: "<<xhlXMLChToString(root->getNodeName())<<endl;

	//////	DOMNodeList* children = root->getChildNodes();
	//////	for (unsigned int i=0; i<children->getLength()-1; i++)
	//////	{
	//////		DOMNode* node = children->item(i);
	//////		if (node->getNodeType()==DOMNode::ELEMENT_NODE)
	//////		{
	//////			DOMElement* element = (DOMElement*)node;
	//////			getElement(element);
	//////		}
	//////	}
	//////	cout<<"\n\n";
	//////	
	//////	string text = "mat2";
	//////	XMLCh* xtext = new XMLCh[text.length() + 1];
	//////	XMLString::transcode(text.c_str(), xtext, text.length());

	//////	DOMElement* element = xmlDoc->getElementById(xtext);
	//////	if (element!=NULL)
	//////	{
	//////		cout<<"ALLAHO AKBR";
	//////	}
	//////	else
	//////	{
	//////		cout<<"NULL";
	//////	}
	//////	
	//////	//cout<<"\n\n\n\n";
	//////	//string text("123");
	//////	//XMLCh* xtext = new XMLCh[text.length() + 1];
	//////	//XMLString::transcode(text.c_str(), xtext, text.length());
	//////	//DOMNodeList* children2 = pDocument->getElementsByTagName(xtext);
	//////	//for (int i=0; i<children2->getLength()-1; i++)
	//////	//{
	//////	//	DOMNode* node = children2->item(i);
	//////	//	cout<<CHelper::XMLCh_to_string(node->getNodeName())<<endl;
	//////	//}

	//////	cout<<"\n\n\n\n";
	//////	//DOMElement* element = pDocument->getElementById(xtext);

	//////}
	//////catch (const XMLException& toCatch) {
	//////	char* message = XMLString::transcode(toCatch.getMessage());
	//////	cout<< "Exception message is: \n"
	//////		<< message << "\n";
	//////	XMLString::release(&message);
	//////	return NULL;
	//////}
	//////catch (const DOMException& toCatch) {
	//////	char* message = XMLString::transcode(toCatch.msg);
	//////	cout<< "Exception message is: \n"
	//////		<< message << "\n";
	//////	XMLString::release(&message);
	//////	return NULL;
	//////}
	//////catch (...) {
	//////	cout<< "Unexpected Exception \n" ;
	//////	return NULL;
	//////}

	//////delete parser;
	//////delete errHandler;
	//////return NULL;

	return 0;
}

void DisplayElement(CXMLElement* element, string prefix = "")
{
	cout<<prefix<<element->getName();
	if (element->hasValue())
		cout<<": "<<element->getStringValue();
	
	
	string m1 = element->getAttributeValue("Material1");
	string m2 = element->getAttributeValue("Material2");
	cout<<"  "<<m1<<"  "<<m2<<endl;
	

	vector<CXMLElement*>* vec = element->getChildElements();
	for(int i=0; i<vec->size(); i++)
	{
		CXMLElement* child = vec->at(i);
		DisplayElement(child, prefix + "\t");
	}
}

void ElementToObject( CXMLElement* element ) 
{
	//vector<CXMLElement*>* vec = element->getChildElements();
	
}

int _tmain(int argc, _TCHAR* argv[])
{
	xhlInitialize();
	
	CXMLDocument* doc1 = CXMLDocument::OpenDocument("sample.xml","sample.xsd");
	//DisplayElement(doc1->GetRootElement());

	CXMLElement* mat3 = doc1->getElementByAttribute("MaterialName","mat3");
	vector<CXMLElement*>* v =  mat3->getChildElements();
	ElementToObject(mat3);
	delete mat3;

	cout<<"done\n\n";
	 mat3 = doc1->getElementByAttribute("MaterialName","mat3");
	 delete mat3;
	

	delete doc1;
	xhlTerminate();

	cout<<"Done\n\n";
	return 0;
}